package com.midea.ai.air.ui.more.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.midea.ai.air.R;
import com.midea.ai.air.common.BaseFragment;
import com.midea.ai.air.common.ConsVal;
import com.midea.ai.air.ui.more.MorePersonNameActivity;
import com.midea.ai.air.ui.more.adapter.MorePersonAdapter;
import com.midea.ai.air.ui.more.data.MorePersonModel;
import com.midea.ai.air.utils.GlideRoundTransform;
import com.midea.ai.air.utils.JumpUtils;
import com.midea.ai.air.view.AlertCenterDialog;
import com.midea.ai.air.view.BottomDialog;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ouyangdi on 2016/4/11.
 * 更多->个人资料Fragment
 */
public class MorePersonFragment extends BaseFragment implements View.OnClickListener{
    private ListView mMsgList;
    private List<MorePersonModel> mPersonMsgs = new ArrayList<>();
    private MorePersonAdapter mAdapter;

    private ImageView mHeadIcon;
    private TextView mLogOut;
    private RelativeLayout mLayoutShowCode;
    private Uri mFileUri;

    private BottomDialog mBottomDialog;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_more_person, container, false);
        initData();
        initViews(view);
        return view;
    }

    private void initData() {
        Resources resources = getResources();
        mPersonMsgs.add(new MorePersonModel(resources.getString(R.string.air_more_person_name), "18877779999"));
        mPersonMsgs.add(new MorePersonModel(resources.getString(R.string.air_more_person_sex), "未设置"));
        mPersonMsgs.add(new MorePersonModel(resources.getString(R.string.air_more_person_address), "未设置"));
        mAdapter = new MorePersonAdapter(getActivity(), mPersonMsgs);

        mFileUri = getOutputMediaFileUri();
    }

    private void initViews(View view) {
        mHeadIcon = (ImageView) view.findViewById(R.id.imgView_icon);
        Glide.with(this).load(R.drawable.air_photo).transform(new GlideRoundTransform(getActivity(), getResources().getDimensionPixelOffset(R.dimen.height_60dp))).into(mHeadIcon);
        mHeadIcon.setOnClickListener(this);

        mLogOut = (TextView) view.findViewById(R.id.tv_log_out);
        mLogOut.setOnClickListener(this);

        mMsgList = (ListView)view.findViewById(R.id.list_person_msg);
        mMsgList.setAdapter(mAdapter);
        mMsgList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        Intent intent = new Intent(getActivity(), MorePersonNameActivity.class);
                        intent.putExtra(ConsVal.KEY_TITLE,getString(R.string.air_more_person_name));
                        getActivity().startActivity(intent);
                        break;
                    case 1:
                        JumpUtils.toPage(getActivity(),getString(R.string.air_more_person_sex), getString(R.string.air_more_person), ConsVal.ACTIVITY_TAG_MORE_PERSON_SEX);
                        break;
                    case 2:
                        JumpUtils.toPage(getActivity(),getString(R.string.air_more_person_address), getString(R.string.air_more_person), ConsVal.ACTIVITY_TAG_MORE_PERSON_ADD);
                        break;
                }
            }
        });
        mLayoutShowCode = (RelativeLayout) view.findViewById(R.id.layout_show_code);
        mLayoutShowCode.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imgView_icon:
                List<BottomDialog.ListItem> items = new ArrayList<>();
                items.add(new BottomDialog.ListItem(getString(R.string.air_more_person_shoot)));
                items.add(new BottomDialog.ListItem(getString(R.string.air_more_person_local)));
                mBottomDialog = new BottomDialog(getActivity()).builder().setTitle(getString(R.string.air_more_person_change_photo)).addListItems(items);
                mBottomDialog.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        switch (position) {
                            case 0: //拍照
                                Intent intentCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//系统已有的相机app
                                intentCapture.putExtra(MediaStore.EXTRA_OUTPUT, mFileUri);
                                getActivity().startActivityForResult(intentCapture, ConsVal.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
                                break;

                            case 1://选择本地图片
                                Intent intentAlbum = new Intent(Intent.ACTION_GET_CONTENT);
                                intentAlbum.setType("image/*");
                                getActivity().startActivityForResult(intentAlbum, ConsVal.GET_CONTENT_ACTIVITY_REQUEST_CODE);
                                break;
                        }
                    }
                });
                mBottomDialog.show();
                break;

            case R.id.tv_log_out:
                AlertCenterDialog logOutDialog = new AlertCenterDialog(getActivity()).builder().setTitle(getString(R.string.air_more_person_log_out))
                        .setMsg(getString(R.string.air_more_person_log_out_confirm))
                        .setPositiveButton("", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        })
                        .setNegativeButton("", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        });
                logOutDialog.show();
                break;

            case R.id.layout_show_code:
                View view = View.inflate(getActivity(), R.layout.layout_person_code, null);
                Dialog dialog = new Dialog(getActivity(), R.style.center_dialog_style);
                dialog.setContentView(view);
                dialog.show();
                break;
        }
    }

    public Uri getFileUri() {
        return mFileUri;
    }

    /** Create a file Uri for saving an image or video */
    private static Uri getOutputMediaFileUri(){
        return Uri.fromFile(getOutputMediaFile());
    }

    /** Create a File for saving an image or video */
    private static File getOutputMediaFile(){
        // To be safe, you should check that the SDCard is mounted
        // using Environment.getExternalStorageState() before doing this.

        /**
         * 图片保存路径
         */
        File mediaStorageDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "midea");

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }

        // Create a media file name
        File mediaFile;
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "photo" + ".jpg");

        return mediaFile;
    }

    public ImageView getHeadIcon() {
        return mHeadIcon;
    }

    /**
     * 关闭底部弹窗
     */
    public void closeBotDialog() {
        if(mBottomDialog != null) {
            mBottomDialog.dismiss();
        }
    }
}
